package com.iqiyi.pps.epg.core.utils;

import com.iqiyi.pps.epg.core.model.channel.ChannelLog;
import com.iqiyi.pps.epg.core.model.program.ProgramLog;
import com.iqiyi.pps.epg.core.model.vod.VodLog;
import com.iqiyi.pps.epg.core.rpc.qipu.QipuService;
import com.iqiyi.pps.epg.core.service.channel.ChannelService;
import com.iqiyi.pps.epg.core.service.notifymail.NotifyMailService;
import com.iqiyi.pps.epg.core.service.program.*;
import com.iqiyi.pps.epg.core.service.record.IRecordAsyncService;
import com.iqiyi.pps.epg.core.service.record.IRecordService;
import com.iqiyi.pps.epg.core.service.record.RecordColService;
import com.iqiyi.pps.epg.core.service.sginal.*;
import com.iqiyi.pps.epg.core.service.statistic.StatisticAutoNewDataService;
import com.iqiyi.pps.epg.core.service.statistic.StatisticOcrNewDataService;
import com.iqiyi.pps.epg.core.service.ticket.ITicketService;
import com.iqiyi.pps.epg.core.service.vod.IVodService;
import com.iqiyi.pps.epg.core.service.vod.IVodadService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by karkarrotxu on 2016/9/5.
 */
public final class WriteSystemLogUtil {
    private static Logger logger = LoggerFactory.getLogger(WriteSystemLogUtil.class);
    private static WriteSystemLogUtil instance = null;

    private WriteSystemLogUtil() {
    }

    public static WriteSystemLogUtil getInstance() {
        if (instance == null) {
            synchronized (WriteSystemLogUtil.class) {
                instance = new WriteSystemLogUtil();
            }
        }
        return instance;
    }

    private ChannelService channelService = null;
    private ProgramService programService = null;
    private QipuService qInstanceService = null;
    private NotifyMailService nInstanceService = null;
    private ISnapAsyncService snapService = null;
    private IProgramAsyncService programAsyncService = null;
    private IProgramDirectorService pdService = null;
    private IProgramDirectorResourceService prService = null;
    private IVodService vodService = null;
    private IVodadService vodadService = null;

    private SginalService sginalService = null;
    private SginalStatusService sginalStatusService = null;
    private SginalLogService sginalLogService = null;
    private ServerService serverService = null;
    private SginalServerService sginalServerService = null;
    private ServerStatusService serverStatusService = null;
    private StatisticOcrNewDataService ocrNewService = null;
    private StatisticAutoNewDataService autoNewService = null;
    private ITicketService ticketService = null;
    private VersionControllerService versionControllerService = null;
    private IRecordService recordService = null;
    private RecordColService recordColService = null;
    private IRecordAsyncService recordAsyncService = null;
    private ServerClusterService serverClusterService = null;

    public SginalService getSginalService(){
        if(sginalService == null){
            sginalService = (SginalService) ApplicationContextUtil.getBean(SginalService.class);
        }
        return sginalService;
    }

    public SginalStatusService getSginalStatusService(){
        if(sginalStatusService == null){
            sginalStatusService = (SginalStatusService) ApplicationContextUtil.getBean(SginalStatusService.class);
        }
        return sginalStatusService;
    }

    public SginalLogService getSginalLogService(){
        if(sginalLogService == null){
            sginalLogService = (SginalLogService) ApplicationContextUtil.getBean(SginalLogService.class);
        }
        return sginalLogService;
    }

    public VersionControllerService getVersionControllerService(){
        if(versionControllerService == null){
            versionControllerService = (VersionControllerService)ApplicationContextUtil.getBean(VersionControllerService.class);
        }
        return versionControllerService;
    }

    public ServerClusterService getServerClusterService(){
        if(serverClusterService == null){
            serverClusterService = (ServerClusterService)ApplicationContextUtil.getBean(ServerClusterService.class);
        }
        return serverClusterService;
    }

    public RecordColService getRecordColService(){
        if(recordColService == null){
            recordColService = (RecordColService)ApplicationContextUtil.getBean(RecordColService.class);
        }
        return recordColService;
    }

    public ServerService getServerService(){
        if(serverService == null){
            serverService = (ServerService) ApplicationContextUtil.getBean(ServerService.class);
        }
        return serverService;
    }

    public SginalServerService getSginalServerService(){
        if(sginalServerService == null){
            sginalServerService = (SginalServerService) ApplicationContextUtil.getBean(SginalServerService.class);
        }
        return sginalServerService;
    }

    public ServerStatusService getServerStatusService(){
        if(serverStatusService == null){
            serverStatusService = (ServerStatusService) ApplicationContextUtil.getBean(ServerStatusService.class);
        }
        return serverStatusService;
    }

    public IVodService getVodService() {
        if (vodService == null) {
            synchronized (this) {
                vodService = (IVodService) ApplicationContextUtil.getBean(IVodService.class);
            }
        }
        return vodService;
    }

    public IVodadService getVodadService() {
        if (vodadService == null) {
            synchronized (this) {
                vodadService = (IVodadService) ApplicationContextUtil.getBean(IVodadService.class);
            }
        }
        return vodadService;
    }


    public IProgramDirectorResourceService getPrService() {
        if (prService == null) {
            prService = (IProgramDirectorResourceService) ApplicationContextUtil.getBean(IProgramDirectorResourceService.class);
        }
        return prService;
    }

    public IProgramDirectorService getPdService() {
        if (pdService == null) {
            pdService = (IProgramDirectorService) ApplicationContextUtil.getBean(IProgramDirectorService.class);
        }
        return pdService;
    }

    public ISnapAsyncService getSnapAsyncService() {
        if (snapService == null) {
            synchronized (this) {
                snapService = (ISnapAsyncService) ApplicationContextUtil.getBean(ISnapAsyncService.class);
            }
        }
        return snapService;
    }

    public ChannelService getChannelService() {
        if (channelService == null) {
            synchronized (this) {
                channelService = (ChannelService) ApplicationContextUtil.getBean(ChannelService.class);
            }
        }
        return channelService;
    }

    public StatisticOcrNewDataService getStatisticOcrNewDataService() {
        if (ocrNewService == null) {
            synchronized (this) {
                ocrNewService = (StatisticOcrNewDataService) ApplicationContextUtil.getBean(StatisticOcrNewDataService.class);
            }
        }
        return ocrNewService;
    }

    public StatisticAutoNewDataService getStatisticAutoNewDataService() {
        if (autoNewService == null) {
            synchronized (this) {
                autoNewService = (StatisticAutoNewDataService) ApplicationContextUtil.getBean(StatisticAutoNewDataService.class);
            }
        }
        return autoNewService;
    }

    public IProgramAsyncService getProgramAsyncService() {
        if (programAsyncService == null) {
            synchronized (this) {
                programAsyncService = (IProgramAsyncService) ApplicationContextUtil.getBean(IProgramAsyncService.class);
            }
        }
        return programAsyncService;
    }

    public ProgramService getProgramService() {
        if (programService == null) {
            synchronized (this) {
                programService = (ProgramService) ApplicationContextUtil.getBean(ProgramService.class);
            }
        }
        return programService;
    }

    public QipuService getQipuService() {
        if (qInstanceService == null) {
            synchronized (this) {
                qInstanceService = (QipuService) ApplicationContextUtil.getBean(QipuService.class);
            }
        }
        return qInstanceService;
    }

    public NotifyMailService getNoticeMailService() {
        if (nInstanceService == null) {
            synchronized (this) {
                nInstanceService = (NotifyMailService) ApplicationContextUtil.getBean(NotifyMailService.class);
            }
        }
        return nInstanceService;
    }

    public ITicketService getTicketService() {
        if (ticketService == null) {
            synchronized (this) {
                ticketService = (ITicketService) ApplicationContextUtil.getBean(ITicketService.class);
            }
        }
        return ticketService;
    }

    public IRecordService getRecordService() {
        if (recordService == null) {
            synchronized (this) {
                recordService = (IRecordService) ApplicationContextUtil.getBean(IRecordService.class);
            }
        }
        return recordService;
    }

    public IRecordAsyncService getRecordAsyncService() {
        if (recordAsyncService == null) {
            synchronized (this) {
                recordAsyncService = (IRecordAsyncService) ApplicationContextUtil.getBean(IRecordAsyncService.class);
            }
        }
        return recordAsyncService;
    }


    public void writeChannelLog(long channelId, int type, String user, String descr) {
        try {
            ChannelLog tChannelLog = new ChannelLog();
            tChannelLog.setFatherId(channelId);
            tChannelLog.setType(type);
            if (user == null) {
                logger.warn("cid={},type={} user empty", channelId, type);
                user = Constants.SYSTEM_USER_SIGN;
            }
            tChannelLog.setUser(user);
            tChannelLog.setDescr(descr);
            getChannelService().saveLog(tChannelLog);
        } catch (Exception e) {
            logger.error("[writeChannelLog][exception=]", e);
        }
    }

    public void writeProgramLog(long programId, int type, String descr) {
        writeProgramLog(programId, type, Constants.SYSTEM_USER_SIGN, descr);
    }

    public void writeProgramLog(long programId, int type, String user, String descr) {
        if (programId <= 0) {
            return;
        }
        try {
            ProgramLog programLog = new ProgramLog();
            programLog.setProgramId(programId);
            programLog.setType(type);
            programLog.setUser(user);
            programLog.setDescription(descr);
            getProgramService().saveProgramLog(programLog);
        } catch (Exception e) {
            logger.error("[writeProgramLog][exception=]", e);
        }
    }

    public void writeVodLog(int channelId, int type, String user, String descr) {
        try {
            VodLog vodLog = new VodLog();
            vodLog.setChannelId(channelId);
            vodLog.setType(type);
            vodLog.setUser(user);
            vodLog.setDescription(descr);
            getVodService().saveLog(vodLog);
        } catch (Exception e) {
            logger.error("[writeVodLog][exception=]", e);
        }
    }

    public ChannelLog getChannelLog(long channelId, int type, String user, String descr) {
        ChannelLog channelLog = new ChannelLog();
        channelLog.setFatherId(channelId);
        channelLog.setDescr(descr);
        channelLog.setUser(user);
        channelLog.setType(type);
        return channelLog;
    }

    public ChannelLog getChannelLog(long channelId, int type, String descr) {
        return getChannelLog(channelId, type, Constants.SYSTEM_USER_SIGN, descr);
    }


    public ProgramLog getProgramLog(long programId, int type, String descr) {
        return getProgramLog(programId, type, Constants.SYSTEM_USER_SIGN, descr);
    }

    public ProgramLog getProgramLog(long programId, int type, String user, String descr) {
        ProgramLog programLog = new ProgramLog();
        programLog.setProgramId(programId);
        programLog.setDescription(descr);
        programLog.setUser(user);
        programLog.setType(type);
        return programLog;
    }
}
